<?php

class incident_public
{
	var $data= Array();
	var $items = Array();

	function incident_public($event)
	{
		global $lang, $db, $properties;
		
		include_once('includes/addMessage.php');
		
		$q = 'SELECT in_id, e_title, c_title,c_id, e_id, d_title, in_raisedtime, in_race, in_lap, in_corner, in_replaytime, in_adminstatus
			  FROM ' . JUDGES_TABLE . ', ' . DIVISIONS_TABLE . ', ' . CHAMPIONSHIPS_TABLE . ', ' . INCIDENTS_TABLE .  ', ' . EVENTS_TABLE .  '
			  	    WHERE (c_id = ia_champ OR ia_champ = 0) AND 
			  		d_champid = c_id and d_id = in_division AND in_event = e_id AND in_event = ' . $event . '
			  GROUP BY in_id
			  ORDER BY d_order ASC, in_race ASC, in_lap ASC, in_id ASC';

		$res = $db->sql_query($q);
		
		if($db->sql_numrows($res) > 0)
		{
			$this->items[] = new addMessage('', $lang['Incidents']);
			
			while ($row = $db->sql_fetchrow($res))
			{
				$row = formatArrayOutput($row, false);
				$this->data[$row['in_id']] = $row;	
				$this->data[$row['in_id']]['in_drivers'] = Array();
				$this->data[$row['in_id']]['in_judgements'] = Array();
				
			}	
			
			//get drivers involved.
			$q = 'SELECT pf_drivername, id_incident, id_driver, id_role
				  FROM  ' . DRIVERNAME_TABLE .  ', ' . INCIDENTS_TABLE .  ', ' . INCIDENTDRIVERS_TABLE .  '
				  WHERE in_id = id_incident AND user_id = id_driver AND in_event =  ' . $event . '
				  ORDER BY in_id ASC, id_role ASC, pf_drivername ASC';
			$res = $db->sql_query($q);
		
			while ($row = $db->sql_fetchrow($res))
			{
				$row = formatArrayOutput($row, false);
				$this->data[$row['id_incident']]['in_drivers'][] = $row;
			}
			
			
			//get judgements.
			$q = 'SELECT ar_incident, ar_explanation, as_ruling, 
						 ar_ptspen, ar_weightpen, ar_driver, dr.pf_drivername as ar_drivername
				  FROM  ' . DRIVERNAME_TABLE .  ' dr, ' . INCIDENTS_TABLE .  ', ' . 
				  			INCIDENTADMINRULINGS_TABLE .  ', ' . INCIDENTADMINREVIEW_TABLE .  '
				  WHERE dr.user_id = ar_driver AND as_id = ar_verdict AND 
				  		ar_incident = in_id AND ar_admin = 0 AND in_event = ' . $event . '
				  ORDER BY in_id ASC, ar_drivername  ASC';
 
			$res = $db->sql_query($q);
		
			while ($row = $db->sql_fetchrow($res))
			{
				$row = formatArrayOutput($row, false);
				$this->data[$row['ar_incident']]['in_judgements'][] = $row;
			}
			
		}
		else
		{
			$this->items[] = new addMessage($lang['IncidentsNone'], $lang['Incidents']);
		}
	}
	
	function render()
	{
		foreach($this->items as $value)
		{
			$value->render();
		}
		
		if( sizeof($this->data) > 0)
		{
			global $lang, $skin_dir;
			
			$type = 'IncidentEvent';
			
			include $skin_dir . 'tpl/tableStart.tpl';	
			include $skin_dir . 'tpl/incidentPublicTitle.tpl';	
				
			$last_event = '';
			$on = 'off';
						
			foreach($this->data as $incident)
			{			
				$incident['title'] =  $incident['d_title'] . ': ' . $lang['ResultsRace'] . ' ' . $incident['in_race'];		
			
				if($last_event == $incident['title'])
				{
					$incident['remStart'] = '<!--';
					$incident['remEnd'] = '-->';
				}
				else
				{
					$incident['remStart'] = '';
					$incident['remEnd'] = '';
				}
				
				$incident['comments'] = '';
				
				$lineBreak = $last_event == '' ? '' : '<br><br>';
				$last_event = $incident['title'];
				$incident['title'] = $lineBreak . $incident['title'];
				
				$incident['id'] = '#'  . $incident['in_id'];
				$incident['details'] = $lang['IncidentLap'] . ': ' . $incident['in_lap'] . '<br>' . $lang['IncidentCorner'] . ': ' . $incident['in_corner']
										. '<br>' . $lang['IncidentReplayTime'] . ': ' . $incident['in_replaytime'];
				
				
				$incident['drivers'] = '';
				$atFault = 0;
				$victim = 0;
				foreach($incident['in_drivers'] as $driver)
				{
					if($atFault == 0 && $driver['id_role'] == 0)
					{
						$incident['drivers'] .= $lang['IncidentFault'][$driver['id_role']] . ': ';
						$atFault = 1;
					}
					else if($victim == 0 && $driver['id_role'] == 1)
					{
						$incident['drivers'] .= '<br>' . $lang['IncidentFault'][$driver['id_role']] . ': ';
						$victim = 1;
					}
					else
					{
						$incident['drivers'] .= ', ';
					}
					
					$incident['drivers'] .= '<a href="drivers.php?id=' . $driver['id_driver'] . '">' . $driver['pf_drivername'] . '</a>';
				}
				
				$incident['casestatus'] = $lang['IncidentStatus'][$incident['in_adminstatus']];
						
				$incident['judgements'] = '';
				$incident['remJudgeStart'] = '';
				$incident['remJudgeEnd'] = '';
				if(sizeof($incident['in_judgements']) > 0)
				{
					foreach($incident['in_judgements'] as $judgement)
					{
						if($incident['in_adminstatus'] == 3)
						{
							$penalty = '';
							if($judgement['ar_ptspen'] > 0  || $judgement['ar_weightpen'] > 0)
							{
								$penalty = '<font color="red">';
								$penalty .= $judgement['ar_ptspen'] > 0 ? $lang['IncidentPointsPenalty'] . ' ' . $judgement['ar_ptspen'] . $lang['ResultsPtsUnit'] : '';
								$penalty .= $judgement['ar_weightpen'] > 0 ? $lang['IncidentWeightPenalty'] . ' ' . $judgement['ar_weightpen'] . $lang['ResultsWeightUnit'] : '';
								$penalty .= '</font>';
							}					
							$incident['judgements'] .= '<a href="drivers.php?id=' . $judgement['ar_driver'] . '">' . $judgement['ar_drivername'] . '</a>, ' 
														. $judgement['as_ruling'] . '.  <i>' . $judgement['ar_explanation'] . '. ' .  $penalty . '</i><br>';
						}
						else
						{
							$incident['remJudgeStart'] = '<!--';
							$incident['remJudgeEnd'] = '-->';
						}
					}
				}
				else
				{
							$incident['remJudgeStart'] = '<!--';
							$incident['remJudgeEnd'] = '-->';
				}
								
				include $skin_dir . 'tpl/incidentPublicItem.tpl';
				
				$on = $on == 'on' ? 'off' : 'on';
							
			}
			
			include $skin_dir . 'tpl/tableEnd.tpl';	
		}
	}
}

class incident_public_driver
{
	var $data= Array();
	var $items = Array();

	function incident_public_driver($driver)
	{
		global $lang, $db, $properties;
		
		include_once('includes/addMessage.php');
		
		$q = 'SELECT in_id, e_title, c_smallTitle, c_id, e_id, d_id, in_event, d_title, t_shortTitle, in_raisedtime, in_race, in_lap, in_corner, in_replaytime, in_adminstatus
			  FROM ' . JUDGES_TABLE . ', ' . DIVISIONS_TABLE . ', ' . CHAMPIONSHIPS_TABLE . ', ' . INCIDENTS_TABLE .  ', ' . EVENTS_TABLE .  ', ' .
			  		   INCIDENTDRIVERS_TABLE . ', ' . TRACKS_TABLE . '
			  	    WHERE (c_id = ia_champ OR ia_champ = 0) AND id_incident = in_id AND t_id = e_track AND
			  		d_champid = c_id and d_id = in_division AND in_event = e_id AND id_driver = ' . $driver . '
			  GROUP BY in_id
			  ORDER BY c_id DESC, in_event DESC, d_order ASC, in_race ASC, in_lap ASC, in_id ASC';

		$res = $db->sql_query($q);
		
		if($db->sql_numrows($res) > 0)
		{
			$this->items[] = new addMessage('', $lang['Incidents']);
			
			while ($incident = $db->sql_fetchrow($res))
			{
				$incident = formatArrayOutput($incident, false);
				$this->data[$incident['in_id']] = $incident;	
				$this->data[$incident['in_id']]['in_drivers'] = Array();
				$this->data[$incident['in_id']]['in_judgements'] = Array();
				
			}	
			
			//get drivers involved.
			$q = 'SELECT pf_drivername, id.id_incident, id.id_driver, id.id_role
				  FROM  ' . DRIVERNAME_TABLE .  ', ' . INCIDENTS_TABLE .  ', ' . INCIDENTDRIVERS_TABLE .  ' id, ' . INCIDENTDRIVERS_TABLE . ' d 
				  WHERE d.id_incident = in_id AND in_id = id.id_incident AND user_id = id.id_driver AND d.id_driver = ' . $driver . '
				  ORDER BY in_id ASC, id_role ASC, pf_drivername ASC';
			
			$res = $db->sql_query($q);
		
			while ($row = $db->sql_fetchrow($res))
			{
				$row = formatArrayOutput($row, false);
				$this->data[$row['id_incident']]['in_drivers'][] = $row;
			}
			
			
			//get judgements.
			$q = 'SELECT ar_incident, ar_explanation, as_ruling, 
						 ar_ptspen, ar_weightpen, ar_driver, dr.pf_drivername as ar_drivername
				  FROM  ' . DRIVERNAME_TABLE .  ' dr, ' . INCIDENTS_TABLE .  ', ' . INCIDENTDRIVERS_TABLE . ', ' .
				  			INCIDENTADMINRULINGS_TABLE .  ', ' . INCIDENTADMINREVIEW_TABLE .  '
				  WHERE dr.user_id = ar_driver AND as_id = ar_verdict AND 
				  		ar_incident = in_id AND ar_admin = 0 AND id_incident = in_id AND id_driver = ' . $driver . '
				  ORDER BY in_id ASC, ar_drivername  ASC';
				  
 
			$res = $db->sql_query($q);
		
			while ($row = $db->sql_fetchrow($res))
			{
				$row = formatArrayOutput($row, false);
				$this->data[$row['ar_incident']]['in_judgements'][] = $row;
			}
			
		}
		else
		{
			$this->items[] = new addMessage($lang['IncidentsNone'], $lang['Incidents']);
		}
	}
	
	function render()
	{
		foreach($this->items as $value)
		{
			$value->render();
		}
		
		if( sizeof($this->data) > 0)
		{
			global $lang, $skin_dir;
			
			$type = 'IncidentEvent';
			
			include $skin_dir . 'tpl/tableStart.tpl';	
			include $skin_dir . 'tpl/incidentPublicTitle.tpl';	
				
			$last_event = '';
			$on = 'off';
						
			foreach($this->data as $incident)
			{			
				$incident['title'] =  '<a href="events.php?id=' . $incident['in_event'] . '&mode=results&race=' . $incident['d_id'] . '-' . $incident['in_race'] . '">' . 
									  $incident['c_smallTitle'] . ' ' . $incident['d_title'] . ': ' . $incident['e_title'] . ' (' . $incident['t_shortTitle'] . ') ' . 
									  $lang['ResultsRace'] . ' ' . $incident['in_race'] . '</a>';		
			
				if($last_event == $incident['title'])
				{
					$incident['remStart'] = '<!--';
					$incident['remEnd'] = '-->';
				}
				else
				{
					$incident['remStart'] = '';
					$incident['remEnd'] = '';
				}
				
				$incident['comments'] = '';
				
				$lineBreak = $last_event == '' ? '' : '<br><br>';
				$last_event = $incident['title'];
				$incident['title'] = $lineBreak . $incident['title'];
				
				$incident['id'] = '#'  . $incident['in_id'];
				$incident['details'] = $lang['IncidentLap'] . ': ' . $incident['in_lap'] . '<br>' . $lang['IncidentCorner'] . ': ' . $incident['in_corner']
										. '<br>' . $lang['IncidentReplayTime'] . ': ' . $incident['in_replaytime'];
				
				
				$incident['drivers'] = '';
				$atFault = 0;
				$victim = 0;
				foreach($incident['in_drivers'] as $driver)
				{
					if($atFault == 0 && $driver['id_role'] == 0)
					{
						$incident['drivers'] .= $lang['IncidentFault'][$driver['id_role']] . ': ';
						$atFault = 1;
					}
					else if($victim == 0 && $driver['id_role'] == 1)
					{
						$incident['drivers'] .= '<br>' . $lang['IncidentFault'][$driver['id_role']] . ': ';
						$victim = 1;
					}
					else
					{
						$incident['drivers'] .= ', ';
					}
					
					$incident['drivers'] .= '<a href="drivers.php?id=' . $driver['id_driver'] . '">' . $driver['pf_drivername'] . '</a>';
				}
				
				$incident['casestatus'] = $lang['IncidentStatus'][$incident['in_adminstatus']];
						
				$incident['judgements'] = '';
				$incident['remJudgeStart'] = '';
				$incident['remJudgeEnd'] = '';
				if(sizeof($incident['in_judgements']) > 0)
				{
					foreach($incident['in_judgements'] as $judgement)
					{
						if($incident['in_adminstatus'] == 3)
						{
							$penalty = '';
							if($judgement['ar_ptspen'] > 0  || $judgement['ar_weightpen'] > 0)
							{
								$penalty = '<font color="red">';
								$penalty .= $judgement['ar_ptspen'] > 0 ? $lang['IncidentPointsPenalty'] . ' ' . $judgement['ar_ptspen'] . $lang['ResultsPtsUnit'] : '';
								$penalty .= $judgement['ar_weightpen'] > 0 ? $lang['IncidentWeightPenalty'] . ' ' . $judgement['ar_weightpen'] . $lang['ResultsWeightUnit'] : '';
								$penalty .= '</font>';
							}					
							$incident['judgements'] .= '<a href="drivers.php?id=' . $judgement['ar_driver'] . '">' . $judgement['ar_drivername'] . '</a>, ' 
														. $judgement['as_ruling'] . '.  <i>' . $judgement['ar_explanation'] . '. ' .  $penalty . '</i><br>';
						}
						else
						{
							$incident['remJudgeStart'] = '<!--';
							$incident['remJudgeEnd'] = '-->';
						}
					}
				}
				else
				{
							$incident['remJudgeStart'] = '<!--';
							$incident['remJudgeEnd'] = '-->';
				}
								
				include $skin_dir . 'tpl/incidentPublicItem.tpl';
				
				$on = $on == 'on' ? 'off' : 'on';
							
			}
			
			include $skin_dir . 'tpl/tableEnd.tpl';	
		}
	}
}
?>